//
//  FeedbackViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/2/27.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "FeedbackViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "DataManager.h"
#import <Masonry.h>


#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
#define kShared [DataManager defaultManager]

@interface FeedbackViewController ()<UITextViewDelegate>
@property (nonatomic,strong) UITextView *feedbackTV;
@property (nonatomic,strong) UILabel *numberLabel;
@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"意见反馈";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(didClickRightBtn:)];
    [self addViews];
}

//页面布局
- (void)addViews
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.feedbackTV = [[UITextView alloc] init];
    self.feedbackTV.layer.cornerRadius = 5;
    self.feedbackTV.layer.borderWidth = 0.5;
    self.feedbackTV.font = [UIFont systemFontOfSize:15];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.feedbackTV];
    [self.feedbackTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.15);
        make.left.offset(kWidth * 0.05);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.9, kHeight * 0.25));
    }];
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"您最少输入10个字";
    label1.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.42);
        make.left.offset(kWidth * 0.05);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.5, kHeight * 0.03));
    }];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"/500";
    label2.font = [UIFont systemFontOfSize:14];
    label2.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.42);
        make.right.offset( -(kWidth * 0.05));
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.2, kHeight * 0.03));
    }];
    
    self.numberLabel = [[UILabel alloc] init];
    self.numberLabel.font = [UIFont systemFontOfSize:14];
    self.numberLabel.text = @"0";
    self.numberLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:self.numberLabel];
    self.feedbackTV.delegate = self;
    self.numberLabel.textAlignment = NSTextAlignmentRight;
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.42);
        make.right.offset(-(kWidth * 0.25));
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.1, kHeight * 0.03));
    }];
    
}

//判断输入的字符个数
- (void)textViewDidChange:(UITextView *)textView {
    NSInteger number = [textView.text length];
    if (number > 500) {
        self.numberLabel.text = @"500";
    }
    else
    {
        self.numberLabel.text = [NSString stringWithFormat:@"%ld",number];
    }
}


//右按钮点击事件
- (void)didClickRightBtn:(UIBarButtonItem *)sender
{
    NSInteger number = [self.feedbackTV.text length];
    if(number < 10)
    {
        [self showLabelWithString:@"您最少要输入10个字" AndBlock:nil];
    }
    else if(number > 500)
    {
        [self showLabelWithString:@"您最多只能输入500字" AndBlock:nil];
    }
    else
    {
        //创建一个对象，将数据赋给对象，然后存放到leancloud中
        AVObject *testObject = [AVObject objectWithClassName:@"Feedback"];
        [testObject setObject:[AVUser currentUser] forKey:@"createdBy"];
        [testObject setObject:self.feedbackTV.text forKey:@"feedback"];
        [testObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if(succeeded)
            {
                [self showAlertOneButtonWithTitle:@"提示" AndMessage:@"反馈成功" AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"确定" AndBlock:^{
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            }
            else
            {
                [self showAlertOneButtonWithTitle:@"错误" AndMessage:@"未知原因，反馈失败" AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"确定" AndBlock:nil];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
